/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

/**
 * <p>Project: javaweb - CountUser
 * <p>Powered by webrx On 2022-03-10 14:40:45
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@WebListener
public class CountUser implements HttpSessionListener , HttpSessionAttributeListener {

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        //HttpSession session = event.getSession();
        //sesstion.setAttribute("ip","abc");
        if("ip".equals(event.getName())){
            System.out.println("有人访问:"+event.getValue());
            System.out.println("--------------------------------------------------------");
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        //
        if(event.getName().equals("user")){
            //session.removeAttribute("user")
            System.out.println(event.getValue()+"退出");
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {

    }

    /**
     * Description: 客户第一次访问服务器时执行此方法
     *
     * @date: 2022/3/10 14:42
     * @params: [javax.servlet.http.HttpSessionEvent]
     * @return: void
     */
    @Override
    public void sessionCreated(HttpSessionEvent se) {

    }

    /**
     * Description: servlet容器，销毁会话时执行此方法
     *
     * @date: 2022/3/10 14:43
     * @params: [javax.servlet.http.HttpSessionEvent]
     * @return: void
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("有人下线");
    }
}
